//
//  S1mpl3MoreRecommendViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "S1mpl3MoreRecommendViewController.h"
#import "TwoToOne.h"
#import "MJRefresh.h"
#import "FitHeader.h"
#import "S1mpl3MoreRecommend.h"
#import "S1mpl3RecommendTableViewCell.h"
#import "TabBarController.h"
#import "S1mpl3AlbumViewController.h"
#import "Model.h"

@interface S1mpl3MoreRecommendViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@end

@implementation S1mpl3MoreRecommendViewController

-(void)getData{
    [TwoToOne getDataByString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=android&pageId=%ld&pageSize=20", _page] BodyString:nil WithDataBlock:^(id data) {
        NSArray *listArray = [data objectForKey:@"list"];
        if (listArray.count == 0) {
            [_tableView.mj_footer endRefreshing];
            _tableView.mj_footer.state = MJRefreshStateNoMoreData;
            return ;
        }
        for (NSDictionary *dic in listArray) {
            S1mpl3MoreRecommend *moreRecommend = [[S1mpl3MoreRecommend alloc] init];
            [moreRecommend setValuesForKeysWithDictionary:dic];
            [_dataSourceArray addObject:moreRecommend];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.84 green:0.23 blue:0 alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
}



-(void)viewWillDisappear:(BOOL)animated{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
}

-(void)loadView{
    [super loadView];
    _page = 1;
    self.dataSourceArray = [NSMutableArray array];
    [self getData];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 * FITHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataSourceArray removeAllObjects];
        _page = 1;
        [self getData];
    }];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getData];
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"小编推荐";
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
}

#pragma mark--tableView代理方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    S1mpl3RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[S1mpl3RecommendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    cell.moreRecommend = [_dataSourceArray objectAtIndex:indexPath.row];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100 * FITHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourceArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_dataSourceArray.count == 0) {
        return;
    }
    S1mpl3AlbumViewController *albumVC = [[S1mpl3AlbumViewController alloc] init];
    albumVC.albumId = [[_dataSourceArray objectAtIndex:indexPath.row] albumId];
    albumVC.myTitle = [[_dataSourceArray objectAtIndex:indexPath.row] title];
    albumVC.moreRecommend = [_dataSourceArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:albumVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
